package org.example.rental.config;

import org.example.rental.web.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
// 实现WebMvcConfigurer接口可以简化开发，但具有一定的侵入性
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;


    // 拦截器 添加拦截器并设定拦截的访问路径， 路径可以通过可变参数设置多个
	/**
 	  * addInterceptor：需要一个实现HandlerInterceptor接口的拦截器实例
 	  * addPathPatterns：用于设置拦截器的过滤路径规则；addPathPatterns("/**")对所有请求都拦截
 	  * excludePathPatterns：用于设置不需要拦截的过滤规则
 	  * 拦截器主要用途：进行用户登录状态的拦截，日志的拦截等。
 	*/
    public void addInterceptors(InterceptorRegistry registry) {
    	// 配置拦截器
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/**")// 需要拦截的路径（/** 拦截所有路径）
            	.excludePathPatterns("/login","/register","/**/home/**");//放行静态资源和登陆资源;

}


	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");
		corsConfiguration.addExposedHeader("Authorization");
		return corsConfiguration;
	}

	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}

}
